package at.fhj.pse.pageobjects;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;


public class ShowIntervalsPage
{
	private WebDriver driver;

	/*
	 * Constructor
	 */

	public ShowIntervalsPage(WebDriver driver2) {
		this.driver = driver2;
	}


	/*
	 * Input field: username:String
	 */
	private String username;
	public String getUsername()
	{
		return username;
	}
	public void setUsername(String username)
	{
		this.username = username;
	}


	/*
	 * Input field: password:String
	 */
	private String password;
	public String getPassword()
	{
		return password;
	}
	public void setPassword(String password)
	{
		this.password = password;
	}

	/*
	 * Action: showIntervals
	 */
	public String showIntervals() throws InterruptedException
	{	
		//driver.findElement(By.cssSelector("td")).click();
		for (int second = 0;; second++) {
			if (second >= 60) fail("timeout");
			try { if ("9004".equals(driver.findElement(By.xpath("//table[@id='showIntervalsForm:detailPanel']/tbody/tr/td[2]")).getText())) break; } catch (Exception e) {}
			Thread.sleep(1000);
		}

		assertEquals("9004", driver.findElement(By.xpath("//table[@id='showIntervalsForm:detailPanel']/tbody/tr/td[2]")).getText());
		for (int second = 0;; second++) {
			if (second >= 60) fail("timeout");
			try { if ("2014-11-28 20:00:00.0".equals(driver.findElement(By.xpath("//table[@id='showIntervalsForm:detailPanel']/tbody/tr[2]/td[2]")).getText())) break; } catch (Exception e) {}
			Thread.sleep(1000);
		}

		assertEquals("2014-11-28 20:00:00.0", driver.findElement(By.xpath("//table[@id='showIntervalsForm:detailPanel']/tbody/tr[2]/td[2]")).getText());
		for (int second = 0;; second++) {
			if (second >= 60) fail("timeout");
			try { if ("2014-11-28 22:00:00.0".equals(driver.findElement(By.xpath("//table[@id='showIntervalsForm:detailPanel']/tbody/tr[3]/td[2]")).getText())) break; } catch (Exception e) {}
			Thread.sleep(1000);
		}

		assertEquals("2014-11-28 22:00:00.0", driver.findElement(By.xpath("//table[@id='showIntervalsForm:detailPanel']/tbody/tr[3]/td[2]")).getText());
		for (int second = 0;; second++) {
			if (second >= 60) fail("timeout");
			try { if ("Lehrveranstaltung".equals(driver.findElement(By.xpath("//table[@id='showIntervalsForm:detailPanel']/tbody/tr[5]/td[2]")).getText())) break; } catch (Exception e) {}
			Thread.sleep(1000);
		}

		assertEquals("Lehrveranstaltung", driver.findElement(By.xpath("//table[@id='showIntervalsForm:detailPanel']/tbody/tr[5]/td[2]")).getText());
		driver.findElement(By.xpath("//tbody[@id='showIntervalsForm:intervalsTable_data']/tr[2]/td")).click();
		for (int second = 0;; second++) {
			if (second >= 60) fail("timeout");
			try { if ("9012".equals(driver.findElement(By.xpath("//table[@id='showIntervalsForm:detailPanel']/tbody/tr/td[2]")).getText())) break; } catch (Exception e) {}
			Thread.sleep(1000);
		}

		assertEquals("9012", driver.findElement(By.xpath("//table[@id='showIntervalsForm:detailPanel']/tbody/tr/td[2]")).getText());
		for (int second = 0;; second++) {
			if (second >= 60) fail("timeout");
			try { if ("2014-11-27 09:00:00.0".equals(driver.findElement(By.xpath("//table[@id='showIntervalsForm:detailPanel']/tbody/tr[2]/td[2]")).getText())) break; } catch (Exception e) {}
			Thread.sleep(1000);
		}

		assertEquals("2014-11-27 09:00:00.0", driver.findElement(By.xpath("//table[@id='showIntervalsForm:detailPanel']/tbody/tr[2]/td[2]")).getText());
		for (int second = 0;; second++) {
			if (second >= 60) fail("timeout");
			try { if ("2014-11-27 18:00:00.0".equals(driver.findElement(By.xpath("//table[@id='showIntervalsForm:detailPanel']/tbody/tr[3]/td[2]")).getText())) break; } catch (Exception e) {}
			Thread.sleep(1000);
		}

		assertEquals("2014-11-27 18:00:00.0", driver.findElement(By.xpath("//table[@id='showIntervalsForm:detailPanel']/tbody/tr[3]/td[2]")).getText());
		for (int second = 0;; second++) {
			if (second >= 60) fail("timeout");
			try { if ("Forschungsarbeit".equals(driver.findElement(By.xpath("//table[@id='showIntervalsForm:detailPanel']/tbody/tr[5]/td[2]")).getText())) break; } catch (Exception e) {}
			Thread.sleep(1000);
		}

		assertEquals("Forschungsarbeit", driver.findElement(By.xpath("//table[@id='showIntervalsForm:detailPanel']/tbody/tr[5]/td[2]")).getText());
		driver.findElement(By.xpath("//tbody[@id='showIntervalsForm:intervalsTable_data']/tr[3]/td")).click();
		for (int second = 0;; second++) {
			if (second >= 60) fail("timeout");
			try { if ("9013".equals(driver.findElement(By.xpath("//table[@id='showIntervalsForm:detailPanel']/tbody/tr/td[2]")).getText())) break; } catch (Exception e) {}
			Thread.sleep(1000);
		}

		assertEquals("9013", driver.findElement(By.xpath("//table[@id='showIntervalsForm:detailPanel']/tbody/tr/td[2]")).getText());
		for (int second = 0;; second++) {
			if (second >= 60) fail("timeout");
			try { if ("2014-11-26 10:00:00.0".equals(driver.findElement(By.xpath("//table[@id='showIntervalsForm:detailPanel']/tbody/tr[2]/td[2]")).getText())) break; } catch (Exception e) {}
			Thread.sleep(1000);
		}

		assertEquals("2014-11-26 10:00:00.0", driver.findElement(By.xpath("//table[@id='showIntervalsForm:detailPanel']/tbody/tr[2]/td[2]")).getText());
		for (int second = 0;; second++) {
			if (second >= 60) fail("timeout");
			try { if ("2014-11-26 14:00:00.0".equals(driver.findElement(By.xpath("//table[@id='showIntervalsForm:detailPanel']/tbody/tr[3]/td[2]")).getText())) break; } catch (Exception e) {}
			Thread.sleep(1000);
		}

		assertEquals("2014-11-26 14:00:00.0", driver.findElement(By.xpath("//table[@id='showIntervalsForm:detailPanel']/tbody/tr[3]/td[2]")).getText());
		for (int second = 0;; second++) {
			if (second >= 60) fail("timeout");
			try { if ("Lehrveranstaltung".equals(driver.findElement(By.xpath("//table[@id='showIntervalsForm:detailPanel']/tbody/tr[5]/td[2]")).getText())) break; } catch (Exception e) {}
			Thread.sleep(1000);
		}
		
		return driver.findElement(By.xpath("//table[@id='showIntervalsForm:detailPanel']/tbody/tr[5]/td[2]")).getText();
	}
	
	/*
	 * Method: getCommentFromInterval
	 */
	public String getCommentFromInterval() {
		return driver.findElement(By.xpath("//*[@id=\"showIntervalsForm:intervalsTable_data\"]/tr[1]/td[2]")).getText();
	}
	
	/*
	 * Method: deleteEntry
	 */
	public void deleteFirstEntry() throws InterruptedException {
		driver.findElement(By.xpath("//*[@id=\"showIntervalsForm:intervalsTable_data\"]/tr[1]/td[1]")).click();
		Thread.sleep(1000);
		driver.findElement(By.xpath("//*[@id=\"showIntervalsForm:j_idt56\"]/span")).click();
		Thread.sleep(1000);
		driver.findElement(By.xpath("//*[@id=\"showIntervalsForm:j_idt59\"]/span[1]")).click();
	}
}
